﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UFrameWork
{
    /// <summary>
    /// 协程启动器
    /// </summary>
    public class AsyncRuner : MonoBehaviour
    {
        private static AsyncRuner instance;

        /// <summary>
        /// 创建对象
        /// </summary>
        public static void Init()
        {
            if(instance == null)
            {
                instance = new GameObject("AsyncRuner").AddComponent<AsyncRuner>();
                instance.gameObject.hideFlags = HideFlags.HideInHierarchy;
                DontDestroyOnLoad(instance.gameObject);
            }
        }

        /// <summary>
        /// 运行协程异步方法
        /// </summary>
        /// <param name="action">异步方法</param>
        /// <returns></returns>
        public static Coroutine Run(IEnumerator action)
        {
            if (instance == null)
            {
                Init();
            }

            return instance.StartCoroutine(action);
        }
    }
}